package com.sec.etech.ism.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.sec.etech.common.constant.DBConstant;
import com.sec.etech.ism.constant.SignatureConstant;
import com.sec.etech.ism.service.IsmCommonService;
import com.sec.etech.ism.service.IsmMethodService;
import com.sec.etech.ism.service.IsmTaskService;
import com.sec.etech.sys.util.DbUtil;
import lombok.extern.slf4j.Slf4j;
import org.openbpm.base.api.exception.BusinessMessage;
import org.openbpm.base.core.util.StringUtil;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Slf4j
@Service
public class IsmGetComDrawingFileServiceImpl implements IsmMethodService {
    @Resource
    IsmTaskService ismTaskService;
    @Resource
    IsmCommonService ismCommonService;

    public IsmGetComDrawingFileServiceImpl(){}

    @Override
    public JSONObject call(String sysCode, String serviceCode, JSONObject businessParams) throws Exception{
        JdbcTemplate slaveJdbcTemplate = DbUtil.getJdbcTemplate(DBConstant.ETECH_SLAVE);
        JSONObject result = new JSONObject();
        String materialNo = this.checkAndGetData(businessParams);
        //查询零件信息
        String sql = "select t1.partsid,t1.partsnum,t1.partsname,t1.parts_SAPnum,t1.generalview,t2.numcode " +
                "from prod_parts t1 join proj_component_route t2 on t1.partsid=t2.partsid " +
                "where t2.active=1 and t2.componentid>0 and t2.componentid=" +
                "(select componentid from prod_component where active=1 and component_SAPnum=?)";
        List<Map<String,Object>> partsList = slaveJdbcTemplate.queryForList(sql, materialNo);
        if(partsList==null || partsList.size()==0){
            return result;
        }
        List<JSONObject> partsDrawingList = new ArrayList<>();
        //获取零件图纸信息
        for(Map<String,Object> map:partsList){
            JSONObject object = new JSONObject();
            object.put("numcode", map.get("numcode")!=null ? map.get("numcode") : "");
            object.put("partsnum", map.get("partsnum"));
            object.put("partsname", map.get("partsname"));
            object.put("partsSAPnum", map.get("parts_SAPnum"));
            object.put("generalview", map.get("generalview")!=null ? map.get("generalview") : 0);
            //获取零件图纸信息
            List<String> drawingList = ismCommonService.getPartsDrawingFile(
                    map.get("partsid").toString(), map.get("partsnum").toString());
            object.put("drawingList", drawingList);
            partsDrawingList.add(object);
        }
        ismTaskService.deleteTaskByParams(sysCode, serviceCode, serviceCode, null, businessParams.getString(SignatureConstant.INPUT_PARAMS));
        result.put("flag", "success");
        result.put("data", partsDrawingList);
        return result;
    }

    //入参检查
    private String checkAndGetData(JSONObject busParams) throws BusinessMessage {
        String materialNo = busParams.getJSONObject(SignatureConstant.INPUT_PARAMS).getString("material_no");
        if(StringUtil.isEmpty(materialNo)){
            throw new BusinessMessage("请求中缺少materialNo参数，请检查！");
        }
        return materialNo;
    }

    @Override
    public String methodName(){
        return "getComDrawingFile";
    }
}
